#include <bits/stdc++.h>

using std::cin;
using std::cout;
using std::min;
using std::vector;

const int infinity = (int)1e9 + 1;

int main() {
    int n, m;
    cin >> n >> m;
    vector <vector <int> > grid(n, vector <int>(m, 0));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> grid[i][j];
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (i + j == 0) {
                continue;
            }
            grid[i][j] += min((i > 0 ? grid[i - 1][j] : infinity), (j > 0 ? grid[i][j - 1] : infinity));
        }
    }
    cout << grid[n - 1][m - 1];
}
